<%@ page import="com.lagou.edu.pojo.Resume" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" %>
<jsp:include page="common.jsp" flush="true"/>
<!doctype html>
<html>
<head>
    <title>列表</title>
    <style>
        .list-wrapper {
            width: 1200px;
            margin: 200px auto;
        }

        .list-wrapper .list-button {
            text-align: right;
        }

        .list-wrapper th {
            width: 20%;
        }
    </style>
</head>
<body onload="onload(${isOnloaded})">
<form id="listForm">
    <input id="actionType" type="hidden" name="actionType" value=""/>
    <input id="methodType" type="hidden" name="_method" value=""/>
</form>
<div class="list-wrapper">
    <div class="list-button">
        <button id="addButton" type="button" class="btn btn-secondary" onclick="add()">新增</button>
        <button id="exitButton" type="button" class="btn btn-secondary" onclick="logout()">注销</button>
    </div>
    <br>
    <table class="table">
        <thead class="thead-dark">
        <tr>
            <th scope="col">id</th>
            <th scope="col">地址</th>
            <th scope="col">姓名</th>
            <th scope="col">电话</th>
            <th scope="col">操作</th>
        </tr>
        </thead>
        <tbody>
        <% if (request != null) {
            List<Resume> resumes = (List<Resume>) request.getAttribute("resumes");
            if (resumes != null) {
                for (Resume resume : resumes) { %>
        <tr>
            <th scope="row"><%=resume.getId()%>
            </th>
            <td><%=resume.getAddress()%>
            </td>
            <td><%=resume.getName()%>
            </td>
            <td><%=resume.getPhone()%>
            </td>
            <td>
                <button id="editButton" type="button" class="btn btn-secondary" onclick="edit(<%=resume.getId()%>)">编辑
                </button>
                <button id="removeButton" class="btn btn-secondary" onclick="remove(<%=resume.getId()%>)">删除</button>
                <%--                <button id="deleteButton" class="btn btn-secondary" onclick="removeByDelete(<%=resume.getId()%>)">delete</button>--%>
            </td>
        </tr>
        <% }
        }
        } %>
        </tbody>
    </table>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js"
        integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
        crossorigin="anonymous"></script>
<script>
    var listForm = document.getElementById("listForm");
    var methodType = document.getElementById("methodType");
    var actionType = document.getElementById("actionType");

    function onload(isOnloaded) {
        if (!isOnloaded) {
            methodType.value = "get";
            listForm.setAttribute("action", "<%=request.getContextPath()%>/resumes/");
            listForm.setAttribute("method", "get");
            listForm.submit();
        }
    }

    function logout() {
        methodType.value = "post";
        actionType.value = "";
        listForm.setAttribute("action", "<%=request.getContextPath()%>/logout/");
        listForm.setAttribute("method", "post");
        listForm.submit();
    };

    function add() {
        methodType.value = "post";
        actionType.value = "";
        listForm.setAttribute("action", "<%=request.getContextPath()%>/resumes/");
        listForm.setAttribute("method", "post");
        listForm.submit();
    };

    function edit(id) {
        methodType.value = "post";
        actionType.value = "";
        listForm.setAttribute("action", "<%=request.getContextPath()%>/resumes/edit/" + id);
        listForm.setAttribute("method", "post");
        listForm.submit();
    };

    function remove(id) {
        if (!confirm("确定删除？")) {
            return false;
        }

        methodType.value = "post";
        actionType.value = "do";
        listForm.setAttribute("action", "<%=request.getContextPath()%>/resumes/remove/" + id);
        listForm.setAttribute("method", "post");
        listForm.submit();
    };

    function removeByDelete(id) {
        if (!confirm("确定删除？")) {
            return false;
        }

        methodType.value = "delete";
        actionType.value = "do";
        listForm.setAttribute("action", "<%=request.getContextPath()%>/resumes/" + id);
        listForm.setAttribute("method", "post");
        listForm.submit();
    };

</script>
</body>
</html>
